package com.hhq.Dao.Impl;

import com.hhq.Dao.DeptDao;
import com.hhq.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class DeptDaoImpl implements DeptDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    public List<Dept> getAllDept() {
        String sql = "select * from dept";
        List<Dept> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<Dept>(Dept.class));
        return list;
    }

    @Override
    public List<Dept> findDeptByName(String name) {
        String sql = "select * from dept where name = ?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Dept.class),name);
    }

    @Override
    public int addDept(Dept dept) {
        String sql = "insert into dept(name, remark ) value(?,?)";
        int num = jdbcTemplate.update(sql,new Object[]{
                dept.getName(),
                dept.getRemark()
        });
        return num;
    }

    @Override
    public void deleteDept(String name) {
        String sql = "delete from dept where name=?";
        jdbcTemplate.update(sql, new Object[]{name});
    }
}
